<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="rowDel"
      >
        <template slot-scope="scope" slot="menu">
          <el-button
            icon="el-icon-check"
            class="getbtn"
            type="text"
            plain
            size="small"
            @click.stop="handleGetdata(scope.row,scope.index)"
          >详情</el-button>
          <el-button
            icon="el-icon-check"
            class="getbtn"
            type="text"
            plain
            size="small"
            :disabled="scope.row.loanRepeatStatus!=0"
            @click.stop="handleGetLoan(scope.row,scope.index)"
          >放款</el-button>
          <!-- @click.stop="handleGetdata(scope.row,scope.index)" -->
        </template>
        <!-- 资管方审核状态 -->
        <template slot="informationManagementStatus" slot-scope="scope">
          <!-- :disabled="btndisabled" -->
          <el-button
            class="shenhe"
            type="text"
            :class="{hide:deptId=='37' || deptId=='38' || deptId=='39'}"
            plain
            size="small"
            v-if="scope.row.informationManagementStatus == '2'"
            @click.stop="auditStatus(scope.row,scope.index)"
          >审核</el-button>
          <span
            :class="{hide:deptId=='40' || scope.row.informationManagementStatus == '1' || scope.row.informationManagementStatus == '0'}"
          >未审核</span>
          <span v-if="scope.row.informationManagementStatus == '0'">已通过</span>
          <span v-if="scope.row.informationManagementStatus == '1'">未通过</span>
        </template>
        <!-- 初审 -->
        <template slot="loanStatus" slot-scope="scope">
          <!-- :disabled="scope.row.informationManagementStatus == '0'?false:true" -->
          <el-button
            class="shenhe"
            type="text"
            :class="{hide:deptId=='40' || deptId=='38' || deptId=='39'}"
            v-if="scope.row.loanStatus == '2'"
            plain
            size="small"
            @click.stop="chuShen(scope.row,scope.index)"
          >审核</el-button>
          <span
            :class="{hide:deptId=='37' || scope.row.loanStatus == '1' || scope.row.loanStatus == '0'}"
          >未审核</span>
          <span v-if="scope.row.loanStatus == '0'">已通过</span>
          <span v-if="scope.row.loanStatus == '1'">未通过</span>
        </template>
        <!-- 复审 -->
        <template slot="loanRepeatStatus" slot-scope="scope">
          <!-- :disabled="scope.row.loanStatus == '0'?false:true" -->
          <el-button
            class="shenhe"
            type="text"
            :class="{hide:deptId=='40' || deptId=='38' || deptId=='39'}"
            plain
            size="small"
            v-if="scope.row.loanRepeatStatus == '2'"
            @click.stop="fuShen(scope.row,scope.index)"
          >审核</el-button>
          <span
            :class="{hide:deptId=='37' || scope.row.loanRepeatStatus == '1' || scope.row.loanRepeatStatus == '0'}"
          >未审核</span>
          <span v-if="scope.row.loanRepeatStatus == '0'">已通过</span>
          <span v-if="scope.row.loanRepeatStatus == '1'">未通过</span>
        </template>
      </avue-crud>
      <!-- 操作详情 -->
      <div class="mask" v-if="isShow" @click.self="maskShow">
        <div>
          <div>
            <div>
              借款订单号：
              <el-input disabled v-model="form.id"></el-input>
            </div>
            <div>
              账户id(关联用户)：
              <el-input disabled v-model="form.accountId"></el-input>
            </div>
          </div>
          <div>
            <div>
              借款人电话
              <el-input disabled v-model="form.loanPhone"></el-input>
            </div>
            <div>
              借款人身份证
              <el-input disabled v-model="form.loanCardNum"></el-input>
            </div>
          </div>
          <div>
            <div>
              借款产品id
              <el-input disabled v-model="form.loanProductId"></el-input>
            </div>
            <div>
              借款金额
              <el-input disabled v-model="form.loanAmount"></el-input>
            </div>
          </div>
          <div>
            <div>
              资管方借款审核状态（0.通过 1.未通过 2.未审核）
              <el-input disabled v-model="form.informationManagementStatus"></el-input>
            </div>
            <div>
              资金方借款初审状态（0.通过 1.未通过 2.未审核）
              <el-input disabled v-model="form.loanStatus"></el-input>
            </div>
          </div>
          <div>
            <div>
              资金方借款复审状态(0.通过 1.未通过 2.未审核）
              <el-input disabled v-model="form.loanRepeatStatus"></el-input>
            </div>
            <div>
              借款时间
              <el-input disabled v-model="form.loanTime"></el-input>
            </div>
          </div>
          <div>
            <div>
              放款时间
              <el-input disabled v-model="form.creditTime"></el-input>
            </div>
            <div>
              放款状态(0.放款通过 1.放款不通过 2.未审核)
              <el-input disabled v-model="form.creditStatus"></el-input>
            </div>
          </div>
          <div>
            <div>
              借款产生的总利息
              <el-input disabled v-model="form.loanTotalInterest"></el-input>
            </div>
            <div>
              借款期数
              <el-input disabled v-model="form.loanPeriodsNum"></el-input>
            </div>
          </div>
          <div>
            <div>
              资金方实际利率
              <el-input disabled v-model="form.capitalRate"></el-input>
            </div>
            <div>
              资金方实际利息
              <el-input disabled v-model="form.capitalInterest"></el-input>
            </div>
          </div>
          <div>
            <div>
              接收还款的账号
              <el-input disabled v-model="form.repayReceiverAccount"></el-input>
            </div>
            <div>
              首付款状态（1.已支付 2.未支付）
              <el-input disabled v-model="form.firstPaymentStatus"></el-input>
            </div>
          </div>
          <div>
            <div>
              首付款金额
              <el-input disabled v-model="form.firstPaymentAmount"></el-input>
            </div>
            <div>
              首付款总金额
              <el-input disabled v-model="form.firstPaymentTotalAmount"></el-input>
            </div>
          </div>
          <div>
            <div>
              借款模块id（关联模块）
              <el-input disabled v-model="form.loanModularId"></el-input>
            </div>
            <div>
              借款创建时间
              <el-input disabled v-model="form.createTime"></el-input>
            </div>
          </div>
          <div>
            <div class="left">
              备注(技术服务费)
              <el-input disabled v-model="form.remarks"></el-input>
            </div>
            <div>
              借款凭证图片
              <div>
                <div class="images" v-for="(item, index) in form.image" :key="index">
                  <!-- <img ref="bigImage" @mousewheel="rollImg(this)"  @click="isBigimg(index)" :src="item" width="110px" height="100px" alt /> -->
                  <el-image
                    style="width: 100px; height: 100px"
                    :src="item"
                    :preview-src-list="form.image"
                  ></el-image>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 资金方借款审核状态 -->
      <div class="maskShow" v-if="shShow" @click.self="shMask">
        <div class="mask-main">
          <div class="content">
            <div>
              借款订单号：
              <el-input disabled v-model="auditStatusFrom.loanId"></el-input>
            </div>
            <div>
              借款人姓名：
              <el-input disabled v-model="auditStatusFrom.name"></el-input>
            </div>
          </div>
          <div class="content">
            <div>
              借款账户
              <el-input disabled v-model="auditStatusFrom.accountId"></el-input>
            </div>
            <div>
              借款金额
              <el-input disabled v-model="auditStatusFrom.loanAmount"></el-input>
            </div>
          </div>
          <div class="image">
            <div>
              <div class="text">借款凭证图片</div>

              <div v-for="(item, index) in auditStatusFrom.images" :key="index">
                <img :src="item" width="120px" height="100px" alt />
              </div>
            </div>
          </div>
        </div>
        <div class="btn">
          <div>
            <el-button style="background:green;color:#fff" @click="shAdopt">通过</el-button>
            <el-button style="background:red;color:#fff" @click="shNotpass">不通过</el-button>
          </div>
        </div>
      </div>
      <!-- 资金方借款初审 -->
      <div class="maskShow" v-if="chushenShow" @click.self="chuShenMask">
        <div class="mask-main">
          <div class="content">
            <div>
              借款订单号：
              <el-input disabled v-model="chushenForm.loanId"></el-input>
            </div>
            <div>
              借款人姓名：
              <el-input disabled v-model="chushenForm.name"></el-input>
            </div>
          </div>
          <div class="content">
            <div>
              借款账户
              <el-input disabled v-model="chushenForm.accountId"></el-input>
            </div>
            <div>
              借款金额
              <el-input disabled v-model="chushenForm.loanAmount"></el-input>
            </div>
          </div>
          <div class="image">
            <div>
              <div class="text">借款凭证图片</div>

              <div v-for="(item, index) in chushenForm.images" :key="index">
                <img :src="item" width="120px" height="100px" alt />
              </div>
            </div>
          </div>
          <div class="btn">
            <div>
              <el-button style="background:green;color:#fff" @click="Adopt">通过</el-button>
              <el-button style="background:red;color:#fff" @click="notPass">不通过</el-button>
            </div>
          </div>
        </div>
      </div>
      <!-- 资金方借款复审 -->
      <div class="maskShow" v-if="fushenShow" @click.self="fuShenMask">
        <div class="mask-main">
          <div class="content">
            <div>
              借款订单号：
              <el-input disabled v-model="fushenForm.loanId"></el-input>
            </div>
            <div>
              首付款状态：
              <el-input disabled v-model="fushenForm.firstPaymentStatus"></el-input>
            </div>
          </div>
          <div class="btn">
            <div>
              <el-button style="background:green;color:#fff" @click="fuShenAdopt">通过</el-button>
              <el-button style="background:red;color:#fff" @click="fuShenNotpass">不通过</el-button>
            </div>
          </div>
        </div>
      </div>
    </basic-container>
  </div>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  phoneObj,
  informationManagement,
  informationManagementAdopt,
  loanStatus,
  loanRepeatStatus,
  loanStatusAdopt,
  loanRepeatStatusAdopt,
  LoanBtn
} from "@/api/insurance/loan";
import { tableOption } from "@/const/crud/insurance/loan";
import { mapGetters } from "vuex";

export default {
  name: "loan",
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      isShow: false,
      shShow: false,
      chushenShow: false,
      fushenShow: false,
      deptId: "",
      rowdata: {},
      current: "",
      form: {
        id: "",
        accountId: "",
        loanName: "",
        loanPhone: "",
        loanCardNum: "",
        loanProductId: "",
        loanAmount: "",
        informationManagementStatus: "",
        loanStatus: "",
        loanRepeatStatus: "",
        loanTime: "",
        creditTime: "",
        creditStatus: "",
        loanTotalInterest: "",
        loanPeriodsNum: "",
        capitalRate: "",
        capitalInterest: "",
        repayReceiverAccount: "",
        firstPaymentStatus: "",
        firstPaymentAmount: "",
        firstPaymentTotalAmount: "",
        loanModularId: "",
        createTime: "",
        remarks: "",
        image: ""
      },
      auditStatusFrom: {
        loanId: "",
        name: "",
        accountId: "",
        loanAmount: "",
        images: []
      },
      chushenForm: {
        loanId: "",
        name: "",
        accountId: "",
        loanAmount: "",
        images: []
      },
      fushenForm: {
        loanId: "",
        firstPaymentStatus: ""
      }
    };
  },
  created() {
    this.deptId = sessionStorage.getItem("deptId");
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.insurance_loan_add, false),
        delBtn: this.vaildData(this.permissions.insurance_loan_del, false),
        editBtn: this.vaildData(this.permissions.insurance_loan_edit, false)
      };
    }
  },
  methods: {
    getList(page, params) {
      this.tableLoading = true;
      let accountId = sessionStorage.getItem("username");

      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize
          },
          params,
          this.searchForm,
          { accountId: accountId }
        )
      )
        .then(response => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    rowDel: function(row, index) {
      var _this = this;
      this.$confirm("是否确认删除ID为" + row.id, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObj(row.id);
        })
        .then(data => {
          _this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
    handleUpdate: function(row, index, done, loading) {
      putObj(row)
        .then(data => {
          this.$message.success("修改成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    handleSave: function(row, done, loading) {
      addObj(row)
        .then(data => {
          this.$message.success("添加成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    searchChange(form) {
      this.searchForm = form;
      this.getList(this.page, form);
    },
    refreshChange() {
      this.getList(this.page);
    },
    handleGetdata(row, index) {
      phoneObj({ loanId: row.id })
        .then(data => {
          let params = {
            id: data.data.id,
            accountId: data.data.accountId,
            loanName: data.data.loanName,
            loanPhone: data.data.loanPhone,
            loanCardNum: data.data.loanCardNum,
            loanProductId: data.data.loanProductId,
            loanAmount: data.data.loanAmount,
            informationManagementStatus: data.data.informationManagementStatus,
            loanStatus: data.data.loanStatus,
            loanRepeatStatus: data.data.loanRepeatStatus,
            loanTime: data.data.loanTime,
            creditTime: data.data.creditTime,
            creditStatus: data.data.creditStatus,
            loanTotalInterest: data.data.loanTotalInterest,
            loanPeriodsNum: data.data.loanPeriodsNum,
            capitalRate: data.data.capitalRate,
            capitalInterest: data.data.capitalInterest,
            repayReceiverAccount: data.data.repayReceiverAccount,
            firstPaymentStatus: data.data.firstPaymentStatus,
            firstPaymentAmount: data.data.firstPaymentAmount,
            firstPaymentTotalAmount: data.data.firstPaymentTotalAmount,
            loanModularId: data.data.loanModularId,
            createTime: data.data.createTime,
            remarks: data.data.remarks,
            image: []
          };
          for (let i = 0; i < data.data.image.length; i++) {
            params.image.push(`data:image/jpg;base64,${data.data.image[i]}`);
          }
          this.form = params;
          this.isShow = true;
          console.log(this.form);
        })
        .catch(() => {});
    },
    // 资金方审核状态
    auditStatus(row, index) {
      this.rowdata = row;
      informationManagement({ loanId: row.id })
        .then(data => {
          this.$message(data.data.msg);
          let params = {
            loanId: data.data.data.loanId,
            name: data.data.data.name,
            accountId: data.data.data.accountId,
            loanAmount: data.data.data.loanAmount,
            images: []
          };
          for (let i = 0; i < data.data.data.images.length; i++) {
            params.images.push(
              `data:image/jpg;base64,${data.data.data.images[i]}`
            );
          }
          this.auditStatusFrom = params;
          console.log(this.auditStatusFrom);

          this.shShow = true;
        })
        .catch(() => {});
    },
    // 资金方审核通过
    shAdopt() {
      let data = this.rowdata;
      informationManagementAdopt({
        loanId: data.id,
        informationManagementStatus: 0
      })
        .then(data => {
          this.$message({
            message: data.data.msg,
            type: "success"
          });
          this.shShow = false;
          this.getList(this.page);
        })
        .catch(() => {});
    },
    // 资金方审核不通过
    shNotpass() {
      let data = this.rowdata;
      informationManagementAdopt({
        loanId: data.id,
        informationManagementStatus: 1
      })
        .then(data => {
          this.$message({
            message: data.data.msg,
            type: "warning"
          });
          this.shShow = false;
          this.getList(this.page);
        })
        .catch(() => {});
    },
    // 资金方初审
    chuShen(row, index) {
      this.rowdata = row;
      loanStatus({ loanId: row.id })
        .then(data => {
          this.$message(data.data.msg);
          let params = {
            loanId: data.data.data.loanId,
            name: data.data.data.name,
            accountId: data.data.data.accountId,
            loanAmount: data.data.data.loanAmount,
            images: []
          };
          for (let i = 0; i < data.data.data.images.length; i++) {
            params.images.push(
              `data:image/jpg;base64,${data.data.data.images[i]}`
            );
          }
          this.chushenForm = params;
          this.chushenShow = true;
        })
        .catch(() => {});
    },
    // 资金方初审通过
    Adopt() {
      let data = this.rowdata;
      loanStatusAdopt({ loanId: data.id, loanStatus: 0 })
        .then(data => {
          this.$message({
            message: data.data.msg,
            type: "success"
          });
          this.chushenShow = false;
          this.getList(this.page);
        })
        .catch(() => {});
    },
    // 资金方初审不通过
    notPass() {
      let data = this.rowdata;
      loanStatusAdopt({ loanId: data.id, loanStatus: 1 })
        .then(data => {
          this.$message({
            message: data.data.msg,
            type: "warning"
          });
          this.chushenShow = false;
          this.getList(this.page);
        })
        .catch(() => {});
    },
    // 资金方复审
    fuShen(row, index) {
      this.rowdata = row;
      loanRepeatStatus({
        loanId: row.id
      })
        .then(data => {
          this.$message(data.data.msg);
          let params = {
            loanId: data.data.data.loanId,
            firstPaymentStatus: data.data.data.firstPaymentStatus
          };
          this.fushenForm = params;
          console.log(data);

          this.fushenShow = true;
        })
        .catch(() => {});
    },
    // 资金方复审通过
    fuShenAdopt(row, index) {
      let data = this.rowdata;
      loanRepeatStatusAdopt({ loanId: data.id, loanRepeatStatus: 0 })
        .then(data => {
          this.$message({
            message: data.data.msg,
            type: "success"
          });
          this.fushenShow = false;
          this.getList(this.page);
        })
        .catch(() => {});
    },
    // 资金方复审不通过
    fuShenNotpass(row, index) {
      let data = this.rowdata;
      loanRepeatStatusAdopt({ loanId: data.id, loanRepeatStatus: 1 })
        .then(data => {
          this.$message({
            message: data.data.msg,
            type: "warning"
          });
          this.fushenShow = false;
          this.getList(this.page);
        })
        .catch(() => {});
    },
    maskShow() {
      this.isShow = false;
    },
    chuShenMask() {
      this.chushenShow = false;
    },
    fuShenMask() {
      this.fushenShow = false;
    },
    shMask() {
      this.shShow = false;
    },
    // 放款按钮
    handleGetLoan(row, index) {
      LoanBtn({ loanId: row.id }).then(data => {
        console.log(data.data, "data.data");
        this.$message({
          message: data.data.msg
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.execution {
  .maskShow {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.8);
    z-index: 200;
    display: flex;
    justify-content: center;
    align-items: center;
    .mask-main {
      width: 66%;
      background: #fff;
      padding: 20px;
      overflow: hidden;
      height: 80%;
      overflow-y: scroll;
      line-height: 30px;
      .content {
        display: flex;
        justify-content: space-around;
        & > div {
          width: 40%;
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-bottom: 20px;
          .el-input {
            width: 66%;
          }
          & > div {
            width: 66%;
          }
        }
      }
      .image {
        width: 100%;
        display: flex;
        & > div {
          flex: 1;
          display: flex;
          align-items: center;
          .text {
            width: 18.5%;
            display: flex;
            justify-content: center;
          }
        }
      }
      .btn {
        display: flex;
        justify-content: space-around;
      }
    }
    & > div::-webkit-scrollbar {
      width: 0;
      height: 0;
      background-color: transparent;
    }
  }
  .mask {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.8);
    z-index: 200;
    display: flex;
    justify-content: center;
    align-items: center;

    & > div {
      width: 66%;
      background: #fff;
      padding: 20px;
      overflow: hidden;
      height: 80%;
      overflow-y: scroll;
      line-height: 30px;
      & > div {
        display: flex;
        justify-content: space-around;
        & > div {
          width: 40%;
          display: flex;
          // align-items: center;
          justify-content: space-between;
          margin-bottom: 20px;
          .el-input {
            width: 66%;
          }
          & > div {
            width: 66%;
            // display: flex;
          }
        }
      }
    }
    .images {
      display: inline-block;
    }
    .images:nth-child(odd) {
      margin-right: 10px;
    }
    /* .image{

      // display: block;
      div{
        display: inline-block;
        div{
          display: inline-block;
        }
      }
      .left{
        display: flex;
        justify-content: flex-start;
      }
    } */
    & > div::-webkit-scrollbar {
      width: 0;
      height: 0;
      background-color: transparent;
    }
  }
  .shenhe {
    width: 86%;
    color: #fff;
    border: 1px solid green;
    background: red;
  }
  .hide {
    display: none;
  }
  .bigImg {
    img {
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      margin: auto;
      width: 500px;
      height: 300px;
      z-index: 100;
    }
  }
  /deep/ .el-image-viewer__close {
    color: #fff;
  }
}
</style>
